//
//  CalendarReminderUtil.m
//  weappios
//
//  Created by chensentao on 2018/8/10.
//  Copyright © 2018年 XiGuangLu. All rights reserved.
//
#import "CalendarReminderUtil.h"
#import <EventKit/EventKit.h>
#import "ViewUtils.h"
#import "SaveHelper.h"

@implementation CalendarReminderUtil

+(void)addEvent:(NSString*)title des:(NSString*)description time:(long long)reminderTime pre:(int)previousMin
{
    //事件市场
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    //6.0及以上通过下面方式写入事件
    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        // the selector is available, so we must be on iOS 6 or newer
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (error)
                {
                    //错误细心
                    // display error message here
                    [ViewUtils showToast:@"添加失败！"];
                }
                else if (!granted)
                {
                    //被用户拒绝，不允许访问日历
                    // display access denied error message here
                    [ViewUtils showToast:@"添加失败！"];
                }
                else
                {
                    // access granted
                    // ***** do the important stuff here *****
                    //事件保存到日历
                    //创建事件
                    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
                    event.title     = [NSString stringWithFormat:@"%@\n%@",title,description];
                    //event.location = @"我在杭州西湖区留和路";
                    //NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
                    //[tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
                    
                    //每天重复
                    EKRecurrenceRule* rule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily interval:1 daysOfTheWeek:nil daysOfTheMonth:nil monthsOfTheYear:nil weeksOfTheYear:nil daysOfTheYear:nil setPositions:nil end:nil];
                    event.recurrenceRules = @[rule];
                    
                    event.startDate = [NSDate dateWithTimeIntervalSince1970:reminderTime];
                    event.endDate   = [NSDate dateWithTimeIntervalSince1970:reminderTime+60.0*60.0];
                    //event.allDay = YES;//是否全天
                    
                    //添加提醒
                    [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -previousMin]];
                    [event setCalendar:[eventStore defaultCalendarForNewEvents]];
                    NSError *err;
                    if([eventStore saveEvent:event span:EKSpanThisEvent error:&err])
                    {
                        [SaveHelper saveStr:@"eventId" vlu:event.eventIdentifier];
                        [ViewUtils showToast:@"成功添加提醒"];
                    }
                }
            });
        }];
    }
}

+(void)removeEvent
{
    NSString* str=[SaveHelper getSaveStr:@"eventId"];
    if(str){
        EKEventStore *eventStore = [[EKEventStore alloc] init];
        EKEvent *event=[eventStore eventWithIdentifier:str];
        NSError *err;
        if([eventStore removeEvent:event span:EKSpanThisEvent error:&err])
        {
            NSLog(@"删除提醒成功");
        }
    }
}

@end
